import http from 'jw-plugins'
import { getToken } from '@/utils/cookies'
import { TokenKey, ApiTokenKey } from '@/const/keys'
import { user, authority, status, system } from '@/api'

const { statusMap, codeMap, logError } = status

http.reset({
  source: [user, authority, system],
  status: {
    statusMap,
    codeMap,
    logError
  }
})

const Http = {}
Object.keys(http).forEach((key) => {
  Http[key] = http[key]
  if (['reset'].indexOf(key) > -1) {
    return
  }
  Http[key] = function () {
    const data = arguments[0] || {}
    const config = arguments[1] || {}
    const tokenList = [
      [TokenKey, getToken(TokenKey) || ''],
      [ApiTokenKey, 'Basic cGlnOnBpZw==']
    ]
    const Config = {
      ...config,
      defaultToken: false,
      tokenList
    }
    return http[key](data, Config)
  }
  Http[key]['url'] = http[key]['url']
  Http[key]['method'] = http[key]['method']
})

export default Http
